// pages/shelvesgoods/shelvesgoods.js
var app = getApp();
var com = require('../../common.js')
Page({
  data: {
    id: '',
    info: {},
    medical: '',
    medicalInfo: '',
    inputText: '',
    sale: 1,
    text: '待审核'
  },
  onLoad: function(options) {
    var _this = this
    _this.setData({
      id: options.id
    })
    _this.onShopInfo()
  },
  input(e) {
    var _this = this
    _this.setData({
      inputText: e.detail.value
    })
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {},
  //获取商品详情
  onShopInfo() {
    var _this = this
    com.link(app.link.item + _this.data.id + '.st', {}, function(res) {
      console.log(res)

      _this.setData({
        info: res.data.data,
        sale: res.data.data.selected.is_sale ? 1 : 0,
      })
      if (res.data.data.selected) {
        console.log('1')
        _this.setData({
          medical: res.data.data.selected.cover_url,
          medicalInfo: res.data.data.selected.cover,
          inputText: res.data.data.selected.postscript,
        })
        if (res.data.data.selected.examine == "succeeded") {
          console.log('2')
          _this.setData({
            text: '审核通过',
            sale: res.data.data.selected.is_sale
          })
          return false;
        }
        if (res.data.data.selected.examine == "failed") {
          console.log('3')
          _this.setData({
            text: '审核已拒绝',
            sale: res.data.data.selected.is_sale
          })
          return false;
        }
        if (res.data.data.selected.examine == "create") {
          console.log('4')
          _this.setData({
            text: '待审核',
            sale: res.data.data.selected.is_sale
          })
          return false;
        }
      } else {
        console.log('999')
        _this.setData({
          text: '待审核',
          sale: res.data.data.selected
        })
      }
    }, 'GET')
  },
  onInfoList(e) {
    var _this = this
    com.link(app.link.itemFall + _this.data.id + '.st', {
      sale: _this.data.sale ? 1 : 0,
      cover: _this.data.medicalInfo,
      postscript: _this.data.inputText
    }, function(res) {
      if (res.data.state) {
        wx.showModal({
          title: '温馨提示',
          content: res.data.message,
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.data.message,
        })
      }
      console.log(res)
    })
  },
  //上传照片方法
  onShowFile(e) {
    var _this = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        _this.setData({
          medical: res.tempFilePaths[0]
        })
        wx.uploadFile({
          url: app.link.temporary, //为后端上传代码方法
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function(res) {
            let img = JSON.parse(res.data)
            console.log(img.response.file.name)
            _this.setData({
              medicalInfo: img.response.file.name
            })
          }
        })
      }
    })
  },
  //保存上下架
  selected(e) {
    console.log(e)
    var _this = this,
      sale = e.detail.value == 1?true:false
    _this.setData({
      sale: sale
    })
  }
})